$(function () {

    $("#jqGrid").jqGrid({
        url: baseURL + 'api/weixinno/list',
        datatype: "json",
        colModel: [			
			{ label: 'id', name: 'id', index: 'id', width: 30, key: true },
            { label: '来源名称', name: 'dnsname', index: 'dnsname', width: 50 },
            { label: '微信号', name: 'wxno', index: 'wxno', width: 50 },
            { label: '图片链接', name: 'picture', index: 'picture', width: 150,formatter: function(value, options, row){
                return value==null?'':'<a target="_blank" href="'+value+'">'+value+'</a>';
            }},
			// { label: '备注', name: 'remark', index: 'remark', width: 80 },
            // { label: '路径', name: 'path', index: 'path', width: 80 },
            { label: '访问链接', name: 'allurl', index: 'allurl', width: 200,formatter: function(value, options, row){
                return '<a target="_blank" href="'+value+'">'+value+'</a>';
            }},
            { label: '创建时间', name: 'createtime', index: 'createTIme', width: 90 },
            { label: '备注', name: 'remark', index: 'remark', width: 40 },
            { label: '操作人', name: 'operater', index: 'operater', width: 60 }
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});


var select2; //select2插件


var vm = new Vue({
	el:'#rrapp',
	data:{
        q:{
            name: null,
			dnsid: null,
			wxnoname: null,
            oldwxno: null //旧微信号 用于更新模版
        },
		showList: true,
		wexnoList: null, //微信号链接下拉列表数据
		wxno:null, //点击修改里面的默认选中的wxno
		title: null,
		weixinNo: {}
	},
    mounted:function(){
        this.$nextTick(function(){
            vm.showDnsList();
            vm.showWxnoSelectList();
		})
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.weixinNo = {};
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
			var url = vm.weixinNo.id == null ? "api/weixinno/save" : "api/weixinno/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.weixinNo),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
			    	    if(r.msg=="微信号模版不存在"){
                            layer.prompt({title: '微信号模版不存在，请输入旧微信号，自动生成模版', formType: 1}, function(pass, index){
                                layer.close(index);
                                vm.q.oldwxno=pass;
                                vm.weixinNo.oldwxno=pass;
                                vm.saveOrUpdate();
                            });
                        }else{
                            alert(r.msg);
                        }
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "api/weixinno/delete",
                    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get(baseURL + "api/weixinno/info/"+id, function(r){
                vm.weixinNo = r.weixinNo;
                select2.val([vm.weixinNo.wxno]).trigger('change');
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
                postData:{
                	'name': vm.q.name,
                    'dnsid':vm.q.dnsid
				},
                page:page
            }).trigger("reloadGrid");
		},
		//初次导入url
        importUrl: function(){
		    if(vm.q.dnsid==null||vm.q.dnsid==''){
                alert("请先选择域名");
                return;
            }
            $.ajax({
                type: "POST",
                url: baseURL + "api/weixinno/importUrl/"+vm.q.dnsid,
                contentType: "application/json",
                success: function(r){
                    if(r.code == 0){
						alert(r.msg);
                    }else{
                        alert(r.msg);
                    }
                }
            });
		},
		//展示下拉框
		showDnsList: function(){
            $.ajax({
                type: "POST",
                url: baseURL + "api/dnsparsing/listAll",
                contentType: "application/json",
                success: function(r){
                    if(r.code == 0){
                    	var html=vm.getSelectModel(r.list);
                    	$("#wx_select").html(html);
                    }else{
                        alert(r.msg);
                    }
                }
            });
		},
		// 拼接下拉列表的html
		getSelectModel: function(da){
			var html='<option value="">全部</option>';
			for(var i=0;i<da.length;i++){
				html+='<option value="'+da[i].id+'">'+da[i].name+'</option>';
			}
			return html;
		},
		// 下拉框改变时
		clickOption: function(){
			var wx_select=$("#wx_select").val();
			vm.q.dnsid=wx_select;
		},
        //展示下拉框
        showWxnoSelectList: function(){
            $.ajax({
                type: "POST",
                url: baseURL + "sys/oss/listAll",
                async: false,
                contentType: "application/json",
                success: function(r){
                    if(r.code == 0){
                    	vm.wexnoList=r.list;
                       // var html=vm.getSelectHtml();
                       // $("#select2div").html(html);
                        select2=$("#select2div").select2();
                        select2.off().on("change",function(){
                            var data = $(this).val();
                            // var picture = $(this).attr("url",);
                            vm.weixinNo.wxno=data;
                            //vm.weixinNo.picture=
                            select2.select2();

                            $("#select2div option:selected").each(function(i,dom){
                                var url=$(this).attr("url");
                                vm.weixinNo.picture=url;
                            });
                        });
                    }else{
                        alert(r.msg);
                    }
                }
            });
        },
        reloadUrl:function(){
        	var dnsid=vm.q.dnsid;

            if(vm.q.dnsid==null||vm.q.dnsid==''){
                alert("请先选择域名");
                return;
            }

        	//刷新url
            $.ajax({
                type: "POST",
                url: baseURL + "api/weixinno/reloadUrl/"+dnsid,
                contentType: "application/json",
                success: function(r){
                    if(r.code == 0){
						alert("刷新成功！");
                    }else{
                        alert(r.msg);
                    }
                }
            });
		}
	}
});